/**
This file is part of dbdjasperreport.

Copyright (C) 2008 Kue Vang aka Kou
Licensed under the Eclipse Public License, Version 1.0; 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html 
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and limitations 
under the License.
**/

import org.apache.tools.ant.util.FileUtils;

import net.sf.jasperreports.engine.JasperReport
import net.sf.jasperreports.engine.JRExporter
import net.sf.jasperreports.engine.JRExporterParameter
import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperPrint
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import net.sf.jasperreports.engine.export.JRCsvExporter
import net.sf.jasperreports.engine.export.JRHtmlExporter
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter
import net.sf.jasperreports.engine.export.JRPdfExporter
import net.sf.jasperreports.engine.export.JRRtfExporter
import net.sf.jasperreports.engine.export.JRTextExporter
import net.sf.jasperreports.engine.export.JRTextExporterParameter
import net.sf.jasperreports.engine.export.JRXlsExporter
import net.sf.jasperreports.engine.export.JRXlsExporterParameter
import net.sf.jasperreports.engine.export.JRXmlExporter
import net.sf.jasperreports.engine.util.JRLoader
import net.sf.jasperreports.engine.JasperRunManager

class JasperViewController {
	WhereClauseService whereClauseService	
	javax.sql.DataSource dataSource
	
	def index = {
		if(session.systemId && session.reportId){
			def conn = dataSource.getConnection()
			byte[] bytes =null
			def paramMap = session.paramMap
			Integer systemId = new Integer(session.systemId)
			Integer reportId = new Integer(session.reportId)
			def rpt = Report.get(reportId)
			def rptSystem = ReportSystem.get(systemId)
			
			def whereClause = whereClauseService.generateWhereClause(paramMap,systemId)
		
			def nonFilterableParam = ReportParameter.withCriteria{
				eq('parameterFilterable','N')
				eq('report.id',reportId)
				eq('parameterEnded','N')
			}
			
			HashMap parameters = whereClauseService.assignParameter(nonFilterableParam)
			parameters.put("SUBREPORT_DIR", rptSystem.reportDirectory)
			parameters.put("REPORT_LOCALE", request.getLocale())
			if(whereClause)
				parameters.put("pWhereClause", whereClause)
			if(rpt.orderByClauseOverride)
				parameters.put("pOrderByClause", rpt.orderByClauseOverride)
			parameters.put("pTitle", rpt.reportTitle)	
		
			try{
				File sourceFile = new File(rptSystem.reportDirectory + rpt.reportName)
				if(sourceFile.canRead()){
					JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile)
					bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, conn)
				}
				else{
					throw new Exception("Unknown Jasper Report File: " + sourceFile)
				}
			}
			finally{
				conn.close()
			}
		
			response.setHeader("Content-Disposition","inline; filename=" + rpt.reportName.replace(".jasper", ".pdf"))
			response.contentType = "application/pdf"
			response.outputStream << bytes
		}
		else{
			response.setHeader("Content-Disposition","inline")
			response.contentType = "text/plain"
			response.outputStream << "Can not generate a report: "+
									 "No Report selected or your report "+
									 "selection has expired."
		}
	}
}